home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP02.ZIP / CHAP02 / SCHMOO / SCHMOO.CPP < prev    next >
C/C++ Source or Header  |  1993-05-02  |  14KB  |  611 lines

  1. /*
  2.  * SCHMOO.CPP
  3.  *
  4.  * WinMain and CSchmooFrame implementations.
  5.  *
  6.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  7.  *
  8.  * Kraig Brockschmidt, Software Design Engineer
  9.  * Microsoft Systems Developer Relations
  10.  *
  11.  * Internet  :  kraigb@microsoft.com
  12.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  13.  */
  14.  
  15.  
  16.  
  17. #include "schmoo.h"
  18.  
  19.  
  20.  
  21.  
  22. /*
  23.  * WinMain
  24.  *
  25.  * Purpose:
  26.  *  Main entry point of application.   Should register the app class
  27.  *  if a previous instance has not done so and do any other one-time
  28.  *  initializations.
  29.  */
  30.  
  31. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pszCmdLine, int nCmdShow)
  32.     {
  33.     LPCSchmooFrame  pFR;
  34.     FRAMEINIT       fi;
  35.     WPARAM          wRet;
  36.  
  37.     //Attempt to allocate and initialize the application
  38.     pFR=new CSchmooFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  39.  
  40.     fi.idsMin=IDS_FRAMEMIN;
  41.     fi.idsMax=IDS_FRAMEMAX;
  42.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  43.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  44.     fi.idStatMenuMin=ID_MENUFILE;
  45.     fi.idStatMenuMax=ID_MENUHELP;
  46.     fi.iPosWindowMenu=WINDOW_MENU;
  47.     fi.cMenus=CMENUS;
  48.  
  49.     //If we can initialize pFR, start chugging messages
  50.     if (pFR->FInit(&fi))
  51.         wRet=pFR->MessageLoop();
  52.  
  53.     delete pFR;
  54.     return wRet;
  55.     }
  56.  
  57.  
  58.  
  59.  
  60. /*
  61.  * CSchmooFrame::CSchmooFrame
  62.  * CSchmooFrame::~CSchmooFrame
  63.  *
  64.  * Constructor Parameters:
  65.  *  hInst           HINSTANCE from WinMain
  66.  *  hInstPrev       HINSTANCE from WinMain
  67.  *  pszCmdLine      LPSTR from WinMain
  68.  *  nCmdShow        int from WInMain
  69.  */
  70.  
  71. CSchmooFrame::CSchmooFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  72.     , LPSTR pszCmdLine, int nCmdShow)
  73.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  74.     {
  75.     UINT        i;
  76.  
  77.     for (i=0; i<5; i++)
  78.         m_hBmpLines[i]=NULL;
  79.  
  80.     return;
  81.     }
  82.  
  83.  
  84. CSchmooFrame::~CSchmooFrame(void)
  85.     {
  86.     UINT        i;
  87.  
  88.     for (i=0; i<5; i++)
  89.         {
  90.         if (NULL!=m_hBmpLines[i])
  91.             DeleteObject(m_hBmpLines[i]);
  92.         }
  93.  
  94.     return;
  95.     }
  96.  
  97.  
  98.  
  99.  
  100. /*
  101.  * CSchmooFrame::CreateCClient
  102.  *
  103.  * Purpose:
  104.  *  Constructs a new client specific to the application.
  105.  *
  106.  * Parameters:
  107.  *  None
  108.  *
  109.  * Return Value:
  110.  *  LPCClient       Pointer to the new client object.
  111.  */
  112.  
  113. LPCClient CSchmooFrame::CreateCClient(void)
  114.     {
  115.     return (LPCClient)(new CSchmooClient(m_hInst));
  116.     }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. /*
  126.  * CSchmooFrame::FRegisterAllClasses
  127.  *
  128.  * Purpose:
  129.  *  Registers all classes used in this application.
  130.  *
  131.  * Parameters:
  132.  *  None
  133.  *
  134.  * Return Value:
  135.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  136.  */
  137.  
  138. BOOL CSchmooFrame::FRegisterAllClasses(void)
  139.     {
  140.     WNDCLASS        wc;
  141.  
  142.     //First let the standard frame do its thing
  143.     if (!CFrame::FRegisterAllClasses())
  144.         return FALSE;
  145.  
  146.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  147.     wc.hInstance     = m_hInst;
  148.     wc.cbClsExtra    = 0;
  149.     wc.lpfnWndProc   = PolylineWndProc;
  150.     wc.cbWndExtra    = CBPOLYLINEWNDEXTRA;
  151.     wc.hIcon         = NULL;
  152.     wc.hCursor       = LoadCursor(NULL, IDC_CROSS);
  153.     wc.hbrBackground = NULL;
  154.     wc.lpszMenuName  = NULL;
  155.     wc.lpszClassName = SZCLASSPOLYLINE;
  156.  
  157.     if (!RegisterClass(&wc))
  158.         return FALSE;
  159.  
  160.     return TRUE;
  161.     }
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. /*
  169.  * CSchmooFrame::FPreShowInit
  170.  *
  171.  * Purpose:
  172.  *  Called from FInit before intially showing the window.  We do whatever
  173.  *  else we want here, modifying nCmdShow as necessary which affects
  174.  *  ShowWindow in FInit.
  175.  *
  176.  * Parameters:
  177.  *  None
  178.  *
  179.  * Return Value:
  180.  *  BOOL            TRUE if this initialization succeeded, FALSE otherwise.
  181.  */
  182.  
  183. BOOL CSchmooFrame::FPreShowInit(void)
  184.     {
  185.     CreateLineMenu();
  186.     CheckLineSelection(IDM_LINESOLID);
  187.     return TRUE;
  188.     }
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. /*
  196.  * CSchmooFrame::CreateLineMenu
  197.  *
  198.  * Purpose:
  199.  *  Initializes the bitmaps used to create the Line menu and replaces
  200.  *  the text items defined in the application resources with these
  201.  *  bitmaps.  Note that the contents of m_hBmpLines must be cleaned
  202.  *  up when the application terminates.
  203.  *
  204.  * Parameters:
  205.  *  None
  206.  *
  207.  * Return Value:
  208.  *  None
  209.  */
  210.  
  211. void CSchmooFrame::CreateLineMenu(void)
  212.     {
  213.     HMENU       hMenu;
  214.     HDC         hDC, hMemDC;
  215.     HPEN        hPen;
  216.     HGDIOBJ     hObj;
  217.     TEXTMETRIC  tm;
  218.     UINT        i, cx, cy;
  219.  
  220.  
  221.     hMenu=GetSubMenu(GetMenu(m_hWnd), 3);   //Line menu.
  222.     hDC=GetDC(m_hWnd);
  223.  
  224.     //Create each line in a menu item 8 chars wide, one char high.
  225.     GetTextMetrics(hDC, &tm);
  226.     cx=tm.tmAveCharWidth*8;
  227.     cy=tm.tmHeight;
  228.  
  229.     //Create a memory DC in which to draw lines, and bitmaps for each line.
  230.     hMemDC=CreateCompatibleDC(hDC);
  231.     ReleaseDC(m_hWnd, hDC);
  232.  
  233.     for (i=0; i<5; i++)
  234.         {
  235.         m_hBmpLines[i]=CreateCompatibleBitmap(hMemDC, cx, cy);
  236.         SelectObject(hMemDC, m_hBmpLines[i]);
  237.  
  238.         PatBlt(hMemDC, 0, 0, cx, cy, WHITENESS);
  239.  
  240.         hPen=CreatePen(i, 1, 0L);           //i==line style like PS_SOLID
  241.         hObj=SelectObject(hMemDC, hPen);
  242.  
  243.         MoveTo(hMemDC, 0,  cy/2);
  244.         LineTo(hMemDC, cx, cy/2);
  245.  
  246.         ModifyMenu(hMenu, IDM_LINEMIN+i, MF_BYCOMMAND | MF_BITMAP
  247.             , IDM_LINEMIN+i, (LPCSTR)MAKELONG(m_hBmpLines[i], 0));
  248.  
  249.         SelectObject(hMemDC, hObj);
  250.         DeleteObject(hPen);
  251.         }
  252.  
  253.     CheckMenuItem(hMenu, IDM_LINESOLID, MF_CHECKED);
  254.     DeleteDC(hMemDC);
  255.  
  256.     return;
  257.     }
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267. /*
  268.  * CSchmooFrame::CreateGizmos
  269.  *
  270.  * Purpose:
  271.  *  Procedure to create all the necessary gizmobar buttons.
  272.  *
  273.  * Parameters:
  274.  *  None
  275.  *
  276.  * Return Value:
  277.  *  UINT            Number of gizmos added to the bar.
  278.  */
  279.  
  280. UINT CSchmooFrame::CreateGizmos(void)
  281.     {
  282.     UINT            iLast;
  283.     UINT            uState=GIZMO_NORMAL;
  284.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  285.     UINT            utEx  =GIZMOTYPE_BUTTONATTRIBUTEEX;
  286.  
  287.     //Insert the standard ones.
  288.     iLast=CFrame::CreateGizmos();
  289.  
  290.     //Insert File Import in the 5th position and account for it in iLast.
  291.     m_pGB->Add(utCmd, 4, IDM_FILEIMPORT, m_dxB, m_dyB, NULL, m_hBmp, 2, uState);
  292.     iLast++;
  293.  
  294.     //Separator
  295.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  296.  
  297.     //For the Background bitmap, preserve our use of black (part of the image)
  298.     m_pGB->Add(utCmd, iLast++, IDM_COLORBACKGROUND, m_dxB, m_dyB, NULL, m_hBmp, 3
  299.                , GIZMO_NORMAL | PRESERVE_BLACK);
  300.  
  301.     m_pGB->Add(utCmd, iLast++, IDM_COLORLINE, m_dxB, m_dyB, NULL, m_hBmp, 4, uState);
  302.  
  303.     //Separator
  304.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  305.  
  306.     //Line styles.
  307.     m_pGB->Add(utEx, iLast++, IDM_LINESOLID,      m_dxB, m_dyB, NULL, m_hBmp, 5, uState);
  308.     m_pGB->Add(utEx, iLast++, IDM_LINEDASH,       m_dxB, m_dyB, NULL, m_hBmp, 6, uState);
  309.     m_pGB->Add(utEx, iLast++, IDM_LINEDOT,        m_dxB, m_dyB, NULL, m_hBmp, 7, uState);
  310.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOT,    m_dxB, m_dyB, NULL, m_hBmp, 8, uState);
  311.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOTDOT, m_dxB, m_dyB, NULL, m_hBmp, 9, uState);
  312.  
  313.     return iLast;
  314.     }
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323. /*
  324.  * CSchmooFrame::OnCommand
  325.  *
  326.  * Purpose:
  327.  *  WM_COMMAND handler for the Schmoo frame window that just processes
  328.  *  the line menu and the color menu leaving the CFrame to do everything
  329.  *  else.
  330.  *
  331.  * Parameters:
  332.  *  hWnd            HWND of the frame window.
  333.  *  wParam          WPARAM of the message.
  334.  *  lParam          LPARAM of the message.
  335.  *
  336.  * Return Value:
  337.  *  LRESULT         Return value for the message.
  338.  */
  339.  
  340. LRESULT CSchmooFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  341.     {
  342.     LPCSchmooDoc    pDoc;
  343.     char            szFile[CCHPATHMAX];
  344.     BOOL            fOK;
  345.     UINT            i, uTemp;
  346.     COLORREF        rgColors[16];
  347.     CHOOSECOLOR     cc;
  348.  
  349.     COMMANDPARAMS(wID, wCode, hWndMsg);
  350.  
  351.     /*
  352.      * Don't bother with anything during first initialization,
  353.      * skipping many GizmoBar notifications.
  354.      */
  355.     if (m_fInit)
  356.         return 0L;
  357.  
  358.     pDoc=(LPCSchmooDoc)m_pCL->ActiveDocument()